package com.logic.landseaserver.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.exception.BusinessException;
import com.logic.common.security.SecurityUtils;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.EntityResponse;
import com.logic.landseaserver.domain.Issue;
import com.logic.landseaserver.domain.Procedure;
import com.logic.landseaserver.domain.RemouldProject;
import com.logic.landseaserver.persistence.read.EntityResponseReadMapper;
import com.logic.landseaserver.persistence.read.IssueReadMapper;
import com.logic.landseaserver.persistence.read.ProcedureReadMapper;
import com.logic.landseaserver.persistence.read.RemouldProjectReadMapper;
import com.logic.landseaserver.persistence.write.EntityResponseWriteMapper;
import com.logic.landseaserver.service.IEntityResponseService;
import com.logic.landseaserver.service.IIssueService;
import com.logic.landseaserver.service.IRemouldProjectService;
import com.logic.landseaserver.ws.dto.EntityResponseDTO;
import com.logic.landseaserver.ws.dto.IssueDTO;
import com.logic.landseaserver.ws.dto.ProjectCheckDTO;
import com.logic.system.domain.SystemFile;
import com.logic.system.service.IFileService;

@Service
public class EntityResponseServiceImpl implements IEntityResponseService {

	@Autowired
	private EntityResponseReadMapper entityResponseReadMapper;

	@Autowired
	private EntityResponseWriteMapper entityResponseWriteMapper;


    @Autowired
    private IFileService fileService;

    @Autowired
    private IIssueService issueServiceImpl;

	@Autowired
	private ProcedureReadMapper procedureReadMapper;

	@Autowired
	IRemouldProjectService remouldProjectServiceImpl;

	@Autowired
	RemouldProjectReadMapper  remouldProjectReadMapper;

	@Autowired
	private IssueReadMapper issueReadMapper;

	@Override
	public void delete(String arg0) throws BusinessException {
	    String[] idArr = arg0.split(",");
	    for (String id : idArr) {
	      Integer entityResponseId = Integer.valueOf(id);
	      EntityResponse entityResponse = (EntityResponse) this.get(entityResponseId);
	      Boolean pass = this.validateForDelete(entityResponse);
	      if (pass) {
	    	  entityResponse.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
	    	  entityResponseWriteMapper.deleteByPrimaryKey(entityResponse);
	      }
	    }

	}

	@Override
	public Object get(Integer arg0) throws BusinessException {
		return entityResponseReadMapper.selectByPrimaryKey(arg0);
	}

	@Override
	public int insert(Object arg0) throws BusinessException {
		Integer id = -1;
		Boolean pass = this.validateForInsert(arg0);
		if (pass) {
			EntityResponse entityResponse = (EntityResponse) arg0;
			entityResponse.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
			entityResponseWriteMapper.insertSelective(entityResponse);
			id = entityResponse.getId();
		}
		return id;
	}

	@Override
	public List<?> query(QueryUtil queryUtil) {
		return entityResponseReadMapper.selectListByQueryUtil(queryUtil);
	}

	@Override
	public void update(Object arg0) throws BusinessException {
		Boolean pass = this.validateForUpdate(arg0);
		if (pass) {
			EntityResponse entityResponse = (EntityResponse) arg0;
			entityResponse.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
			entityResponse.setCreatedBy(null);
			entityResponse.setCreatedDtm(null);
			entityResponseWriteMapper.updateByPrimaryKeySelective(entityResponse);
		}

	}

	@Override
	public Boolean validate(Object arg0) throws BusinessException {
		Boolean pass = true;
		return pass;
	}

	@Override
	public Boolean validateForDelete(Object arg0) throws BusinessException {
		return true;
	}

	@Override
	public Boolean validateForInsert(Object arg0) throws BusinessException {
		Boolean pass = false;
		pass = this.validate(arg0);
		return pass;
	}

	@Override
	public Boolean validateForUpdate(Object arg0) throws BusinessException {
		Boolean pass = false;
		pass = this.validate(arg0);
		return pass;
	}


	@Override
	public EntityResponseDTO getDTO(Integer id) throws BusinessException {
		EntityResponseDTO entityResponseDTO = entityResponseReadMapper.selectDTOByPrimaryKey(id);
		if (entityResponseDTO != null) {
			this.code2Desc(entityResponseDTO);
		}
		return entityResponseDTO;
	}

	@Override
	public List<EntityResponseDTO> queryDTO(QueryUtil queryUtil) {
		return entityResponseReadMapper.selectDTOListByQueryUtil(queryUtil);
	}

	@Override
	public List<EntityResponseDTO> queryDTOByIssueId(Integer issueId) throws BusinessException {
		List<EntityResponseDTO> list = entityResponseReadMapper.selectDTOListByIssueId(issueId);
		handleFile(list);
		return list;
	}

	private void handleFile(List<EntityResponseDTO> list) throws BusinessException {
		if (list != null && list.size() > 0) {
			for (EntityResponseDTO entityResponseDTO : list) {
				if (entityResponseDTO != null) {
					 this.code2Desc(entityResponseDTO);
					 String images = entityResponseDTO.getImageId();
					 if (images != null) {
						 String[]  imageIds = images.split(",");
						 List<SystemFile> systemFileList = new ArrayList<>();
						 for (String imageId : imageIds) {
							 SystemFile systemFile = (SystemFile) fileService.get(imageId);
							 systemFileList.add(systemFile);
						 }
						 entityResponseDTO.setImageList(systemFileList);
					 }
				}
			}
		}
	}

	private void code2Desc(EntityResponseDTO entityResponseDTO) {
		String result = entityResponseDTO.getResult();
		if ("0".equalsIgnoreCase(result)) {
			entityResponseDTO.setResultDesc("不通过");
		} else if ("1".equalsIgnoreCase(result)) {
			entityResponseDTO.setResultDesc("通过");
		}
		String type = entityResponseDTO.getType();
		if ("1".equalsIgnoreCase(type)) {
			entityResponseDTO.setTypeDesc("问题检查");
		} else if ("2".equalsIgnoreCase(type)) {
			entityResponseDTO.setTypeDesc("项目检查");
		}else if ("3".equalsIgnoreCase(type)) {
			entityResponseDTO.setTypeDesc("项目验收");
		}
	}

	@Override
	public ProjectCheckDTO queryProjectCheckDTOByCheckId(Integer checkId) throws BusinessException {
		ProjectCheckDTO projectCheckDTO = new ProjectCheckDTO();
		Procedure procedure = procedureReadMapper.getByCheckId(checkId);
		if (procedure != null) {
			Integer procedureId = procedure.getId();
			RemouldProject remouldProject = remouldProjectReadMapper.selectByProcedureId(procedureId);
			projectCheckDTO.setProcedureId(procedureId);
			projectCheckDTO.setProcedureName(procedure.getName());
			projectCheckDTO.setProcedureSerialNum(procedure.getSerialNum());
			projectCheckDTO.setProjectCheckList(entityResponseReadMapper.selectDTOListByCheckId(checkId));
			projectCheckDTO.setProjectId(remouldProject.getId());
			projectCheckDTO.setProjectName(remouldProject.getName());
			projectCheckDTO.setProjectSerialNum(remouldProject.getSerialNum());
			List<IssueDTO> issueList = issueReadMapper.listByCheckId(checkId);
			if (issueList != null && issueList.size() > 0) {
				for (IssueDTO issueDTO : issueList) {
					if (issueDTO != null) {
						List<EntityResponseDTO> issueCheckList = issueDTO.getIssueCheckList();
						handleFile(issueCheckList);
					}
				}
			}
			projectCheckDTO.setIssueList(issueList);
		}
		return projectCheckDTO;
	}

	@Override
	public List<EntityResponseDTO> queryDTOByCheckId(Integer checkId) throws BusinessException {
		List<EntityResponseDTO> list = entityResponseReadMapper.selectDTOListByCheckId(checkId);
		handleFile(list);
		return list;
	}

	@Override
	public List<EntityResponseDTO> queryDTOByProjectId(Integer projectId) throws BusinessException {
		List<EntityResponseDTO> list = entityResponseReadMapper.selectDTOListByProjectId(projectId);
		handleFile(list);
		return list;
	}

}
